/*package br.com.digicade.geosite.gpsavl.web.security;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.Principal;

import javax.inject.Inject;
import javax.ws.rs.core.SecurityContext;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.protocol.Protocol;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import br.com.digicade.commons.model.vo.UsuarioVO;
import br.com.digicade.commons.util.DgProperties;
import br.com.digicade.geosite.gpsavl.ssl.EasySSLProtocolSocketFactory;

import com.google.gson.Gson;
import com.google.inject.servlet.SessionScoped;

@SessionScoped
public final class DgSecurityContext implements SecurityContext {

	private UsuarioVO usuario;
	@Inject private DgProperties properties;
        
    @Override
	public final Principal getUserPrincipal() {
		return usuario;
	}

    @Override
	public final boolean isUserInRole(String role) {
		return false;
	}

    @Override
	public final boolean isSecure() {
		return usuario != null;
	}

    @Override
	public final String getAuthenticationScheme() {
		return null;
	}

	public final boolean login(String usuario, String password) throws Exception {
		CloseableHttpClient httpclient = HttpClients.createDefault();
		CloseableHttpResponse response = null;
        try {
            Protocol easyhttps = new Protocol("https", new EasySSLProtocolSocketFactory(), 443);
    	    Protocol.registerProtocol("https", easyhttps);
    	    String urlPost = getWebserviceUrl() + "/rest/usuario/login";

    	    HttpClient client = new HttpClient();
                PostMethod httpPost = new PostMethod(urlPost);
                httpPost.addParameter("usuario", usuario);
                httpPost.addParameter("senha", password);
                httpPost.addParameter("idProduto", getIdProduto());
                httpPost.addParameter("idEsquema", getIdEsquema());
                httpPost.setRequestHeader("Content-Type","application/json; charset=utf-8");
                int result = client.executeMethod(httpPost);
                httpPost.getResponseBody();

                //VALIDAR LDAP
                
                
                
                if (result == HttpStatus.SC_OK) {
                    StringRequestEntity requestEntity = new StringRequestEntity("Content-Type","application/json","UTF-8");
                    httpPost.setRequestEntity(requestEntity);
                    InputStream in = httpPost.getResponseBodyAsStream();

                    InputStreamReader isr = new InputStreamReader(in);

                    StringBuilder sb = new StringBuilder();
                    BufferedReader br = new BufferedReader(isr);
                    String read = br.readLine();
                    
                    while(read != null){
                        sb.append(read);
                        read = br.readLine();
                    }
                    
                    Gson json = new Gson();
                    this.usuario = json.fromJson(sb.toString(), UsuarioVO.class);
                } else {
                    return false;
                }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(response != null){
                response.close();
            }
            httpclient.close();
        }
        return true;
	}

    public final boolean logout() throws Exception {
		CloseableHttpClient httpclient = HttpClients.createDefault();
		CloseableHttpResponse response = null;
        try {
            Protocol easyhttps = new Protocol("https", new EasySSLProtocolSocketFactory(), 443);
    	    Protocol.registerProtocol("https", easyhttps);
    	    String urlPost = getWebserviceUrl() + "/rest/usuario/logout";

    	    HttpClient client = new HttpClient();
			PostMethod httpPost = new PostMethod(urlPost);
			httpPost.addParameter("chave", this.usuario.getChave());
			httpPost.setRequestHeader("Content-Type","application/json; charset=utf-8");
			int result = client.executeMethod(httpPost);
			httpPost.getResponseBody();
			
			if (result == HttpStatus.SC_OK) {
				StringRequestEntity requestEntity = new StringRequestEntity("Content-Type","application/json","UTF-8");
				httpPost.setRequestEntity(requestEntity);
				InputStream in = httpPost.getResponseBodyAsStream();
				
				InputStreamReader isr = new InputStreamReader(in);
			    StringBuilder sb = new StringBuilder();
			    BufferedReader br = new BufferedReader(isr);
			    String read = br.readLine();
			    while(read != null){
			        sb.append(read);
			        read = br.readLine();
			    }  
				return Boolean.valueOf(sb.toString());
			} else {
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			if(response != null){
				response.close();
			}
            httpclient.close();
        }
        return true;
	}

	public final boolean keepAlive() throws Exception {
		CloseableHttpClient httpclient = HttpClients.createDefault();
		CloseableHttpResponse response = null;
        try {
            Protocol easyhttps = new Protocol("https", new EasySSLProtocolSocketFactory(), 443);
    	    Protocol.registerProtocol("https", easyhttps);
    	    String urlPost = getWebserviceUrl() + "/rest/sessao/keepAlive";

    	    HttpClient client = new HttpClient();
			PostMethod httpPost = new PostMethod(urlPost);
			httpPost.addParameter("chave", this.usuario.getChave());
			httpPost.setRequestHeader("Content-Type","application/json; charset=utf-8");
			int result = client.executeMethod(httpPost);
			httpPost.getResponseBody();
			
			if (result == HttpStatus.SC_OK) {
				StringRequestEntity requestEntity = new StringRequestEntity("Content-Type","application/json","UTF-8");
				httpPost.setRequestEntity(requestEntity);
				InputStream in = httpPost.getResponseBodyAsStream();
				
				InputStreamReader isr = new InputStreamReader(in);
			    StringBuilder sb = new StringBuilder();
			    BufferedReader br = new BufferedReader(isr);
			    String read = br.readLine();
			    while(read != null){
			        sb.append(read);
			        read = br.readLine();
			    }  
				return Boolean.valueOf(sb.toString());
			} else {
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			if(response != null){
				response.close();
			}
            httpclient.close();
        }
        return true;
	}

    private String getWebserviceUrl() {
    	return properties.getProperties("dgsecurity.properties").getProperty("webservice.url");
	}
	
	private String getIdProduto() {
		return properties.getProperties("dgsecurity.properties").getProperty("produto.id");
	}
	
	private String getIdEsquema() {
		return properties.getProperties("dgsecurity.properties").getProperty("esquema.id");
	}

}
*/